package com.manager.service.system;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.common.tool.ConsId;
import org.common.tool.MdfiveUtil;
import org.common.tool.SortingMap;
import org.common.util.TranslateHelper;
import org.jorm.Session;
import org.jorm.Transaction;

import com.build.SessionBuilder;
import com.manager.model.SysAdmin;
import com.manager.model.SysAdminRole;
import com.manager.model.SysEvent;
import com.manager.model.SysLink;
import com.manager.model.SysRole;
import com.manager.model.SysRoleEvent;
import com.manager.model.SysRoleLink;
import com.manager.model.dao.SysAdminDao;
import com.manager.model.dao.SysAdminRoleDao;
import com.manager.model.dao.SysEventDao;
import com.manager.model.dao.SysLinkDao;
import com.manager.model.dao.SysRoleDao;
import com.manager.model.dao.SysRoleEventDao;
import com.manager.model.dao.SysRoleLinkDao;
import com.manager.model.dao.impl.SysAdminDaoImpl;
import com.manager.model.dao.impl.SysAdminRoleDaoImpl;
import com.manager.model.dao.impl.SysEventDaoImpl;
import com.manager.model.dao.impl.SysLinkDaoImpl;
import com.manager.model.dao.impl.SysRoleDaoImpl;
import com.manager.model.dao.impl.SysRoleEventDaoImpl;
import com.manager.model.dao.impl.SysRoleLinkDaoImpl;
import com.manager.service.ServiceUtil;
import com.web.tag.bean.PageModel;

public class AdminService {

	public SysAdmin login(String userId, String pwd) {
		return login(userId, pwd, true);
	}

	/**
	 * 管理员登录
	 * 
	 * @param userId
	 * @param pwd
	 * @return
	 */
	public SysAdmin login(String userId, String pwd, boolean md5) {
		// MD5
		if (md5)
			pwd = MdfiveUtil.md5_32(pwd);
		SysAdmin admin = null;
		try {
			Session session = SessionBuilder.currentSession();
			SysAdminDao adminDao = new SysAdminDaoImpl();
			String sql = "select * from sys_admin a where a.login_name = ? and a.login_pass = ? and state = 1";
			admin = adminDao.queryForObject(sql, new String[] { userId, pwd });
			if (admin != null) {
				SysRoleDao roleDao = new SysRoleDaoImpl();
				sql = "select sr.* from sys_role sr inner join sys_admin_role ar on ar.roleId = sr.id where sr.state = 1 and ar.adminId = ?";
				List<SysRole> roles = roleDao.queryForList(sql, new Integer[] { admin.getId() });
				admin.setRoles(roles);
				String roleIds = null;
				try {
					roleIds = new ConsId<Integer, SysRole>(roles).makeIds("getId");
				} catch (Exception e) {
					e.printStackTrace();
				}
				SysEventDao eventDao = new SysEventDaoImpl();
				sql = "select se.* from sys_event se inner join sys_role_event re on re.eventId = se.id where se.state = 1 and re.roleId in (" + roleIds
						+ ") order by se.orderIndex ";
				List<SysEvent> events = eventDao.queryForList(sql);
				admin.setEvents(events);
				SysLinkDao linkDao = new SysLinkDaoImpl();
				sql = "select sl.* from sys_link sl inner join sys_role_link rl on rl.linkId = sl.id where sl.state = 1 and rl.roleId in (" + roleIds + ") order by sl.id ";
				List<SysLink> links = linkDao.queryForList(sql);
				admin.setLinks(links);
			}
			session.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return admin;
	}

	/**
	 * 所有管理员列表
	 * 
	 * @return
	 */
	public List<SysAdmin> findAllAdmin(PageModel model) {
		List<SysAdmin> admins = null;
		try {
			Session session = SessionBuilder.currentSession();
			SysAdminDao adminDao = new SysAdminDaoImpl();
			String sql = "select * from sys_admin order by id desc ";
			if (model != null) {
				try {
					model.setRecodeZong(adminDao.getResultCount("select count(*) as ct from sys_admin", 1).intValue());
				} catch (SQLException e) {
					e.printStackTrace();
				}
//				sql = sql + "limit " + model.getCountPage() * (model.getPageNum() - 1) + "," + model.getCountPage();
				int row_x = model.getCountPage() * model.getPageNum();
				int row_y = (model.getPageNum() - 1) * model.getCountPage();
				sql = "select * from ( select row_.*, rownum rownum_ from (" + sql + ") row_ where rownum <= " + row_x + ") where rownum_ > " + row_y;
			}
			admins = adminDao.queryForList(sql);
			if (admins != null) {
				String adminIds = null;
				try {
					adminIds = new ConsId<Integer, SysAdmin>(admins).makeIds("getId");
				} catch (Exception e) {
					e.printStackTrace();
				}
				SysRoleDao roleDao = new SysRoleDaoImpl();
				sql = "select sr.*,ar.adminId from sys_role sr inner join sys_admin_role ar on ar.roleId = sr.id where sr.state = 1 and ar.adminId in (" + adminIds + ")";
				List<SysRole> roles = roleDao.queryForList(sql);
				if (roles != null) {
					Map<Integer, List<SysRole>> roleMap = null;
					try {
						roleMap = new SortingMap<Integer, SysRole>(roles).listSort("getAdminId");
					} catch (Exception e) {
						e.printStackTrace();
					}
					if (roleMap != null) {
						for (int i = 0; i < admins.size(); i++) {
							SysAdmin admin = admins.get(i);
							admin.setRoles(roleMap.get(admin.getId()));
						}
					}
				}
			}
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return admins;
	}

	/**
	 * 修改管理员信息
	 * 
	 * @param admin
	 * @return
	 */
	public boolean updateAdminInfo(SysAdmin admin) {
		if (admin == null)
			return false;
		boolean state = false;
		try {
			Session session = SessionBuilder.currentSession();
			SysAdminDao adminDao = new SysAdminDaoImpl();
			SysAdmin obj = adminDao.queryForObject("select * from " + SysAdmin.TABLE_NAME + " where " + SysAdmin.COLUMN_ID + " = " + admin.getId());
			if (obj == null)
				return false;
			obj.setName(admin.getName());
			state = adminDao.update(obj) > 0;
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	/**
	 * 管理员修改密码
	 * 
	 * @param admin
	 * @param oldPwd
	 * @return
	 */
	public boolean updateAdminPwd(SysAdmin admin, String oldPwd) {
		if (admin == null)
			return false;
		boolean state = false;
		try {
			Session session = SessionBuilder.currentSession();
			SysAdminDao adminDao = new SysAdminDaoImpl();
			SysAdmin obj = adminDao.queryForObject("select * from " + SysAdmin.TABLE_NAME + " where " + SysAdmin.COLUMN_ID + " = " + admin.getId());
			if (obj == null)
				return false;
			if (obj.getLoginPass().equals(MdfiveUtil.md5_32(oldPwd))) {
				obj.setLoginPass(MdfiveUtil.md5_32(admin.getLoginPass()));
				state = adminDao.update(obj) > 0;
			}
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	/**
	 * 保存或更新管理员信息
	 * 
	 * @param admin
	 * @param updatePwd
	 * @return
	 */
	public boolean saveAdmin(SysAdmin admin, boolean updatePwd) {
		if (admin == null)
			return false;
		boolean state = false;
		try {
			Session session = SessionBuilder.currentSession();
			SysAdminDao adminDao = new SysAdminDaoImpl();
			SysAdmin obj_o = adminDao.queryForObject("select * from " + SysAdmin.TABLE_NAME + " where " + SysAdmin.COLUMN_ID + " = " + admin.getId());
			admin.setLoginPass(MdfiveUtil.md5_32(admin.getLoginPass()));
			if (obj_o == null) {
				admin.setId(new ServiceUtil(session).getNextId(SysAdmin.TABLE_NAME, SysAdmin.COLUMN_ID));
				state = adminDao.save(admin) > 0;
			} else {
				if (!updatePwd) {
					admin.setLoginPass(obj_o.getLoginPass());
				}
				admin.setRegIp(obj_o.getRegIp());
				admin.setRegTime(obj_o.getRegTime());
				state = adminDao.update(admin) > 0;
			}
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	/**
	 * 删除管理员
	 * 
	 * @param adminId
	 * @return
	 */
	public boolean deleteAdmin(int adminId) {
		boolean state = false;
		try {
			Session session = SessionBuilder.currentSession();
			SysAdminDao adminDao = new SysAdminDaoImpl();
			SysAdmin obj_o = adminDao.queryForObject("select * from " + SysAdmin.TABLE_NAME + " where " + SysAdmin.COLUMN_ID + " = " + adminId);
			state = adminDao.delete(obj_o) > 0;
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	/**
	 * 系统所有操作事件
	 * 
	 * @param model
	 * @return
	 */
	public List<SysEvent> findAllEvent(PageModel model, int state) {
		List<SysEvent> events = null;
		try {
			Session session = SessionBuilder.currentSession();
			SysEventDao eventDao = new SysEventDaoImpl();
			String whereSql = state == -1 ? "" : "where se.state = " + state;
			String sql = "select * from sys_event se " + whereSql + " order by se.pid,se.orderIndex ";
			if (model != null) {
				try {
					model.setRecodeZong(eventDao.getResultCount("select count(*) as ct from sys_event se " + whereSql, 1).intValue());
				} catch (SQLException e) {
					e.printStackTrace();
				}
//				sql = sql + "limit " + model.getCountPage() * (model.getPageNum() - 1) + "," + model.getCountPage();
				int row_x = model.getCountPage() * model.getPageNum();
				int row_y = (model.getPageNum() - 1) * model.getCountPage();
				sql = "select * from ( select row_.*, rownum rownum_ from (" + sql + ") row_ where rownum <= " + row_x + ") where rownum_ > " + row_y;
			}
			events = eventDao.queryForList(sql);
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return events;
	}

	public List<SysEvent> findAllEvent(PageModel model) {
		return findAllEvent(model, -1);
	}

	/**
	 * 保存或更新系统事件
	 * 
	 * @param event
	 * @return
	 */
	public boolean saveEvent(SysEvent event) {
		boolean state = false;
		try {
			Session session = SessionBuilder.currentSession();
			ServiceUtil sessionUtil = new ServiceUtil(session);
			SysEventDao eventDao = new SysEventDaoImpl();
			if (event.getId() == null || event.getId() <= 0) {
				event.setId(sessionUtil.getNextId(SysEvent.TABLE_NAME, SysEvent.COLUMN_ID));
			}
			state = eventDao.saveOrUpdate(event) > 0;
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	/**
	 * 删除系统事件
	 * 
	 * @param id
	 * @return
	 */
	public boolean deleteEvent(int id) {
		boolean state = false;
		try {
			Session session = SessionBuilder.currentSession();
			SysEventDao eventDao = new SysEventDaoImpl();
			SysEvent obj = eventDao.queryForObject("select * from " + SysEvent.TABLE_NAME + " where " + SysEvent.COLUMN_ID + " = " + id);
			state = eventDao.delete(obj) > 0;
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	/**
	 * 所有系统角色
	 * 
	 * @param model
	 * @return
	 */
	public List<SysRole> findAllRole(PageModel model, int state) {
		List<SysRole> roles = null;
		try {
			Session session = SessionBuilder.currentSession();
			SysRoleDao roleDao = new SysRoleDaoImpl();
			String whereSql = state == -1 ? "" : "where sr.state = " + state;
			String sql = "select * from sys_role sr " + whereSql + " order by sr.id desc ";
			if (model != null) {
				try {
					model.setRecodeZong(roleDao.getResultCount("select count(*) as ct from sys_role sr " + whereSql, 1).intValue());
				} catch (SQLException e) {
					e.printStackTrace();
				}
//				sql = sql + "limit " + model.getCountPage() * (model.getPageNum() - 1) + "," + model.getCountPage();
				int row_x = model.getCountPage() * model.getPageNum();
				int row_y = (model.getPageNum() - 1) * model.getCountPage();
				sql = "select * from ( select row_.*, rownum rownum_ from (" + sql + ") row_ where rownum <= " + row_x + ") where rownum_ > " + row_y;
			}
			roles = roleDao.queryForList(sql);
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return roles;
	}

	public List<SysRole> findAllRole(PageModel model) {
		return findAllRole(model, -1);
	}

	/**
	 * 保存系统角色信息
	 * 
	 * @param role
	 * @return
	 */
	public boolean saveRole(SysRole role) {
		boolean state = false;
		try {
			Session session = SessionBuilder.currentSession();
			ServiceUtil sessionUtil = new ServiceUtil(session);
			SysRoleDao roleDao = new SysRoleDaoImpl();
			if (role.getId() == null || role.getId() <= 0) {
				role.setId(sessionUtil.getNextId(SysRole.TABLE_NAME, SysRole.COLUMN_ID));
			}
			state = roleDao.saveOrUpdate(role) > 0;
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	/**
	 * 删除系统角色
	 * 
	 * @param id
	 * @return
	 */
	public boolean deleteRole(int id) {
		boolean state = false;
		try {
			Session session = SessionBuilder.currentSession();
			SysRoleDao roleDao = new SysRoleDaoImpl();
			SysRole obj = roleDao.queryForObject("select * from " + SysRole.TABLE_NAME + " where " + SysRole.COLUMN_ID + " = " + id);
			state = roleDao.delete(obj) > 0;
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	/**
	 * 查询角色+事件
	 * 
	 * @param roleId
	 * @return
	 */
	public SysRole findRoleEvent(int roleId) {
		SysRole role = null;
		try {
			Session session = SessionBuilder.currentSession();
			SysRoleDao roleDao = new SysRoleDaoImpl();
			role = roleDao.queryForObject("select * from " + SysRole.TABLE_NAME + " where " + SysRole.COLUMN_ID + " = " + roleId);
			if (role != null) {
				SysEventDao eventDao = new SysEventDaoImpl();
				String sql = "select se.* from sys_event se inner join sys_role_event re on re.eventId = se.id where se.state = 1 and re.roleId = ?";
				List<SysEvent> events = eventDao.queryForList(sql, new Integer[] { roleId });
				role.setEvents(events);
			}
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return role;
	}

	/**
	 * 保存角色事件设置
	 * 
	 * @param roleId
	 * @param eventIds
	 * @return
	 */
	public boolean saveRoleEvent(int roleId, String[] eventIds) {
		boolean state = true;
		if (roleId <= 0)
			return false;
		try {
			Session session = SessionBuilder.currentSession();
			ServiceUtil sessionUtil = new ServiceUtil(session);
			Transaction tran = session.beginTransaction();
			SysRoleDao roleDao = new SysRoleDaoImpl();
			String sql = "delete from sys_role_event where roleId = ?";
			try {
				roleDao.executeUpdate(sql, new Integer[] { roleId });
				if (eventIds == null)
					return true;
				SysRoleEventDao reDao = new SysRoleEventDaoImpl();
				for (int i = 0; i < eventIds.length; i++) {
					SysRoleEvent sre = new SysRoleEvent(0, roleId, TranslateHelper.parseInteger(eventIds[i]));
					sre.setId(sessionUtil.getNextId(SysRoleEvent.TABLE_NAME, SysRoleEvent.COLUMN_ID));
					state = state ? reDao.save(sre) > 0 : false;
				}
				if (state)
					tran.commit();
				else
					tran.rollback();
			} catch (Exception e) {
				tran.rollback();
			}
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	/**
	 * 查询管理员以及拥有角色
	 * 
	 * @param adminId
	 * @return
	 */
	public SysAdmin findAdminRole(int adminId) {
		SysAdmin admin = null;
		try {
			Session session = SessionBuilder.currentSession();
			SysAdminDao adminDao = new SysAdminDaoImpl();
			admin = adminDao.queryForObject("select * from " + SysAdmin.TABLE_NAME + " where " + SysAdmin.COLUMN_ID + " = " + adminId);
			if (admin != null) {
				SysRoleDao roleDao = new SysRoleDaoImpl();
				String sql = "select sr.* from sys_role sr inner join sys_admin_role ar on ar.roleId = sr.id where sr.state = 1 and ar.adminId = ?";
				List<SysRole> roles = roleDao.queryForList(sql, new Integer[] { admin.getId() });
				admin.setRoles(roles);
			}
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return admin;
	}

	/**
	 * 保存管理员角色设置
	 * 
	 * @param adminId
	 * @param roleIds
	 * @return
	 */
	public boolean saveAdminRole(int adminId, String[] roleIds) {
		boolean state = true;
		if (adminId <= 0)
			return false;
		try {
			Session session = SessionBuilder.currentSession();
			ServiceUtil sessionUtil = new ServiceUtil(session);
			Transaction tran = session.beginTransaction();
			SysAdminRoleDao adminRoleDao = new SysAdminRoleDaoImpl();
			String sql = "delete from sys_admin_role where adminId = ?";
			try {
				adminRoleDao.executeUpdate(sql, new Integer[] { adminId });
				if (roleIds == null)
					return true;
				SysAdminRoleDao arDao = new SysAdminRoleDaoImpl();
				for (int i = 0; i < roleIds.length; i++) {
					SysAdminRole sar = new SysAdminRole(0, adminId, TranslateHelper.parseInteger(roleIds[i]));
					sar.setId(sessionUtil.getNextId(SysAdminRole.TABLE_NAME, SysAdminRole.COLUMN_ID));
					state = state ? arDao.save(sar) > 0 : false;
				}
				if (state)
					tran.commit();
				else
					tran.rollback();
			} catch (Exception e) {
				tran.rollback();
			}
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	/**
	 * 根据ID查询admin实例
	 * 
	 * @param adminId
	 * @return
	 */
	public SysAdmin findAdminById(int adminId) {
		SysAdmin admin = null;
		try {
			Session session = SessionBuilder.currentSession();
			SysAdminDao adminDao = new SysAdminDaoImpl();
			admin = adminDao.queryForObject("select * from " + SysAdmin.TABLE_NAME + " where " + SysAdmin.COLUMN_ID + " = " + adminId);
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return admin;
	}

	/**
	 * 根据ID查询Event实例
	 * 
	 * @param eventId
	 * @return
	 */
	public SysEvent findEventById(int eventId) {
		SysEvent event = null;
		try {
			Session session = SessionBuilder.currentSession();
			SysEventDao eventDao = new SysEventDaoImpl();
			event = eventDao.queryForObject("select * from " + SysEvent.TABLE_NAME + " where " + SysEvent.COLUMN_ID + " = " + eventId);
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return event;
	}

	/**
	 * 根据Id查询Role实例
	 * 
	 * @param roleId
	 * @return
	 */
	public SysRole findRoleById(int roleId) {
		SysRole role = null;
		try {
			Session session = SessionBuilder.currentSession();
			SysRoleDao roleDao = new SysRoleDaoImpl();
			role = roleDao.queryForObject("select * from " + SysRole.TABLE_NAME + " where " + SysRole.COLUMN_ID + " = " + roleId);
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return role;
	}

	/**
	 * 查询全部Link(Url)
	 * 
	 * @param model
	 * @param state
	 * @return
	 */
	public List<SysLink> findAllLink(PageModel model, int state) {
		List<SysLink> links = null;
		try {
			Session session = SessionBuilder.currentSession();
			SysLinkDao linkDao = new SysLinkDaoImpl();
			String whereSql = state == -1 ? "" : "where sl.state = " + state;
			String sql = "select * from sys_link sl " + whereSql + " order by sl.id desc ";
			if (model != null) {
				try {
					model.setRecodeZong(linkDao.getResultCount("select count(*) as ct from sys_link sl " + whereSql, 1).intValue());
				} catch (SQLException e) {
					e.printStackTrace();
				}
//				sql = sql + "limit " + model.getCountPage() * (model.getPageNum() - 1) + "," + model.getCountPage();
				int row_x = model.getCountPage() * model.getPageNum();
				int row_y = (model.getPageNum() - 1) * model.getCountPage();
				sql = "select * from ( select row_.*, rownum rownum_ from (" + sql + ") row_ where rownum <= " + row_x + ") where rownum_ > " + row_y;
			}
			links = linkDao.queryForList(sql);
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return links;
	}

	public List<SysLink> findAllLink(PageModel model) {
		return findAllLink(model, -1);
	}

	/**
	 * 根据ID查询Link实例
	 * 
	 * @param linkId
	 * @return
	 */
	public SysLink findLinkById(int linkId) {
		SysLink link = null;
		try {
			Session session = SessionBuilder.currentSession();
			SysLinkDao linkDao = new SysLinkDaoImpl();
			link = linkDao.queryForObject("select * from " + SysLink.TABLE_NAME + " where " + SysLink.COLUMN_ID + " = " + linkId);
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return link;
	}

	/**
	 * 保存或更新Link
	 * 
	 * @param link
	 * @return
	 */
	public boolean saveLink(SysLink link) {
		boolean state = false;
		try {
			Session session = SessionBuilder.currentSession();
			ServiceUtil sessionUtil = new ServiceUtil(session);
			SysLinkDao linkDao = new SysLinkDaoImpl();
			if (link.getId() == null || link.getId() <= 0) {
				link.setId(sessionUtil.getNextId(SysLink.TABLE_NAME, SysLink.COLUMN_ID));
			}
			state = linkDao.saveOrUpdate(link) > 0;
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	/**
	 * 删除Link，ID
	 * 
	 * @param linkId
	 * @return
	 */
	public boolean deleteLink(int linkId) {
		boolean state = false;
		try {
			Session session = SessionBuilder.currentSession();
			SysLinkDao linkDao = new SysLinkDaoImpl();
			SysLink obj = linkDao.queryForObject("select * from " + SysLink.TABLE_NAME + " where " + SysLink.COLUMN_ID + " = " + linkId);
			state = linkDao.delete(obj) > 0;
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	/**
	 * 设置角色拥有Links
	 * 
	 * @param roleId
	 * @param linkIds
	 * @return
	 */
	public boolean saveRoleLink(int roleId, String[] linkIds) {
		boolean state = true;
		if (roleId <= 0)
			return false;
		try {
			Session session = SessionBuilder.currentSession();
			ServiceUtil sessionUtil = new ServiceUtil(session);
			Transaction tran = session.beginTransaction();
			SysRoleLinkDao roleLinkDao = new SysRoleLinkDaoImpl();
			String sql = "delete from sys_role_link where roleId = ?";
			try {
				roleLinkDao.executeUpdate(sql, new Integer[] { roleId });
				if (linkIds == null)
					return true;
				SysRoleLinkDao rlDao = new SysRoleLinkDaoImpl();
				for (int i = 0; i < linkIds.length; i++) {
					SysRoleLink srl = new SysRoleLink(0, roleId, TranslateHelper.parseInteger(linkIds[i]));
					srl.setId(sessionUtil.getNextId(SysRoleLink.TABLE_NAME, SysRoleLink.COLUMN_ID));
					state = state ? rlDao.save(srl) > 0 : false;
				}
				if (state)
					tran.commit();
				else
					tran.rollback();
			} catch (Exception e) {
				tran.rollback();
			}
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	/**
	 * 查询角色+Link(Url)
	 * 
	 * @param roleId
	 * @return
	 */
	public SysRole findRoleLink(int roleId) {
		SysRole role = null;
		try {
			Session session = SessionBuilder.currentSession();
			SysRoleDao roleDao = new SysRoleDaoImpl();
			role = roleDao.queryForObject("select * from " + SysRole.TABLE_NAME + " where " + SysRole.COLUMN_ID + " = " + roleId);
			if (role != null) {
				SysLinkDao linkDao = new SysLinkDaoImpl();
				String sql = "select sl.* from sys_link sl inner join sys_role_link rl on rl.linkId = sl.id where sl.state = 1 and rl.roleId = ?";
				List<SysLink> links = linkDao.queryForList(sql, new Integer[] { roleId });
				role.setLinks(links);
			}
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return role;
	}

}
